/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye.ui.activity.task_list;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.*;
import android.widget.AdapterView;
import android.widget.ListView;
import com.devoxy.roboeye.R;
import com.devoxy.roboeye.core.provider.Task;
import com.devoxy.roboeye.global.IntentHelper;
import com.devoxy.roboeye.ui.DialogHelper;
import com.devoxy.roboeye.ui.NotificationHelper;
import com.devoxy.roboeye.ui.activity.main.RoboEyeListActivity;
import com.devoxy.roboeye.ui.adapter.RoboEyeCursorAdapter;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 25.06.11
 * Time: 20:44
 */
public class TaskListActivity extends RoboEyeListActivity {

    private Cursor tasks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_list);
        getListView().setOnCreateContextMenuListener(this);
        initProperties(savedInstanceState);
        initBehavior(savedInstanceState);
        initUI(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        tasks.requery();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        openTextFile();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.task_list_options, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.clear_finished:
                onClearFinishedClick(findViewById(R.id.clear_finished));
                return true;

            case R.id.cancel_all:
                onCancelAllClick(findViewById(R.id.cancel_all));
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
        getMenuInflater().inflate(R.menu.task_list_context, contextMenu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem menuItem) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) menuItem.getMenuInfo();
        switch (menuItem.getItemId()) {
            case R.id.cancel:
                cancelTaskById(menuInfo.id);
                return true;

            case R.id.photo:
                openImageFile();
                return true;

            case R.id.text:
                openTextFile();
                return true;

            default:
                return super.onOptionsItemSelected(menuItem);
        }
    }

    private void cancelTaskById(long id) {
        if (tasks.getInt(tasks.getColumnIndex(Task.STARTED)) == 1) {
            NotificationHelper.showText(this, R.string.cannot_cancel_started_task);
        } else {
            Intent i = IntentHelper.getCancelTaskIntent(id);
            startService(i);
        }
    }

    private void openImageFile() {
        String path = tasks.getString(tasks.getColumnIndex(Task.SOURCE));
        Intent i = IntentHelper.getOpenImageIntent(path);
        if (IntentHelper.isIntentAvailable(i)) {
            startActivity(i);
        } else {
            NotificationHelper.showText(this, R.string.you_have_not_image_viewer);
        }
    }

    private void openTextFile() {
        if (tasks.getInt(tasks.getColumnIndex(Task.FINISHED)) == 1) {
            String path = tasks.getString(tasks.getColumnIndex(Task.TARGET));
            Intent i = IntentHelper.getOpenTextIntent(path);
            if (IntentHelper.isIntentAvailable(i)) {
                startActivity(i);
            } else {
                NotificationHelper.showText(this, R.string.you_have_not_text_viewer);
            }
        } else {
            NotificationHelper.showText(this, R.string.only_finished_tasks_have_text);
        }
    }

    public void onClearFinishedClick(View v) {
        Intent i = IntentHelper.getClearFinishedTasksIntent();
        startService(i);
    }

    public void onCancelAllClick(View v) {
        Dialog d = DialogHelper.cancelAllTasksDialog(this,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = IntentHelper.getCancelAllIntent();
                        startService(i);
                    }
                },
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }
        );
        d.show();
    }

    @Override
    public void initProperties(Bundle bundle) {
        tasks = getContentResolver().query(Task.CONTENT_URI, null, null, null, Task.CREATE_TS + " DESC");
        startManagingCursor(tasks);
    }

    @Override
    public void initBehavior(Bundle bundle) {
        findViewById(R.id.actionbar_home).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initUI(Bundle bundle) {
        RoboEyeCursorAdapter adapter = new RoboEyeCursorAdapter(this,
                R.layout.task_list_item,
                tasks,
                new String[]{Task.SOURCE, Task.TARGET},
                new int[]{R.id.from, R.id.to});
        adapter.addRule(new TaskStateRule());
        adapter.addRule(new TaskTypeRule());
        setListAdapter(adapter);
    }
}
